package jech913.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import jech913.model.MemberVO;
import jech913.util.OracleJDBC;

/* 회원가입, 로그인, 회원정보 출력 */
public class MemberDAO {
	protected static String joinMember="insert into memberv3(mno,userid, passwd, name, email)"
										+ "values(mbseq.nextval, ?,?,?,?)";
	//로그인여부
	protected static String loginMember="select count(mno) from memberv3 where userid = ? and passwd = ?";
	protected static String showMember="select * from memberv3 where userid = ?";
	
	//회원가입 처리 메서드
	//회원가입 성공여부: result 변수
	//1)가입ok: result에 true 저장 
	//2)가입no: result에 false 저장
	public boolean joinMember(MemberVO mvo){
		Connection conn = null;
		PreparedStatement ps = null;
		boolean result = false;		
		
		try{
			conn = OracleJDBC.OpenConn();
			ps = conn.prepareStatement(joinMember);
			ps.setString(1, mvo.getUserid());
			ps.setString(2, mvo.getPasswd());
			ps.setString(3, mvo.getName());
			ps.setString(4, mvo.getEmail());
			
			//1)
			if(ps.executeUpdate() > 0) result = true;
			
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			OracleJDBC.closeConn(conn, ps, null);
		}return result;
	}
	
	
	//로그인 처리 메서드: 
	//로그인 성공여부 - result변수
	//1) 아이디, 비번확인ok: result에 true저장
	//2) 아이디, 비번확인no: result에 false저장(초기값)
	//sql문 실행해보면 아이디 존재하면 count: 1 존재하지않으면 count:0 
	public boolean loginMember(MemberVO mvo){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		boolean result = false;
		
		try{
			conn = OracleJDBC.OpenConn();
			ps = conn.prepareStatement(loginMember);
			ps.setString(1, mvo.getUserid());
			ps.setString(2, mvo.getPasswd());
			rs = ps.executeQuery();
			
			if(rs.next()){
				//rs.getInt(1)이 즉 count(mno) 값 count=1(아이디, 비번 일치) 0(불 일치)
				//select count(mno)from member
				if(rs.getInt(1) >0) result = true;
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			OracleJDBC.closeConn(conn, ps, null);
		}return result;
	}
	
	//회원정보 조회 메서드
	//세션변수로 선언된 userid를 이용해서 데이터베이스에서 로그인한 회원의 상세정보를 읽어
	//MemberVO 객체에 저장 
	
	public MemberVO showMember(String uid){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		MemberVO mvo = null;
		
		
		try{
			conn = OracleJDBC.OpenConn();
			ps = conn.prepareStatement(showMember);
			ps.setString(1, uid);
			rs = ps.executeQuery();
			
			
			
			if(rs.next()){
				mvo = new MemberVO();
				mvo.setName(rs.getString(4));
				mvo.setEmail(rs.getString(5));
				mvo.setRegdate(rs.getString(6));
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			OracleJDBC.closeConn(conn, ps, rs);
			
		}
		
		return mvo;
		
	}
	
	
}
